home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / parsingcommandline / example5.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  7KB  |  231 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Parsing Command Line        Tulevagen 22       */
  8. /* File:    Example5.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-03-06                                       */
  11. /* Version: 1.0                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how you can create your own strings  */
  21. /* (command lines) which you then can parse with help of the      */
  22. /* ReadArgs() function. We create a RDArgs structure as in the    */
  23. /* last example, but this time we initialize the "RDA_Source"     */
  24. /* field with our own command line. When we later call ReadArgs() */
  25. /* it will notice that it already have a string to parse, and it  */
  26. /* will therefore use that string and not read one from the       */
  27. /* default input handler.                                         */
  28.  
  29.  
  30.  
  31. /* Include the dos library definitions: */
  32. #include <dos/dos.h>
  33.  
  34. /* Include information about the argument parsing routine: */
  35. #include <dos/rdargs.h>
  36.  
  37. /* Now we include the necessary function prototype files:         */
  38. #include <clib/dos_protos.h>       /* General dos functions...    */
  39. #include <clib/exec_protos.h>      /* System functions...         */
  40. #include <stdio.h>                 /* Std functions [printf()...] */
  41. #include <stdlib.h>                /* Std functions [exit()...]   */
  42. #include <string.h>                /* Std functions [srtlen()...] */
  43.  
  44.  
  45.  
  46. /* Here is our command line template: */
  47. #define MY_COMMAND_LINE_TEMPLATE "SoundFile/A,V=Volume/K/N,F=Filter/S"
  48.  
  49. /* Three command templates are used: */
  50. #define NUMBER_COMMAND_TEMPLATES 3
  51.  
  52. /* The command template numbers: (Where the result of each */
  53. /* command template can be found in the "arg_array".)      */
  54. #define SOUNDFILE_TEMPLATE  0
  55. #define VOLUME_TEMPLATE     1
  56. #define FILTER_TEMPLATE     2
  57.  
  58.  
  59.  
  60. /* Set name and version number: */
  61. UBYTE *version = "$VER: AmigaDOS/ParsingCommandLine/Example5 1.0";
  62.  
  63.  
  64.  
  65. /* Declare an external global library pointer to the Dos library: */
  66. extern struct DosLibrary *DOSBase;
  67.  
  68.  
  69.  
  70. /* Declare a pointer to a RDArgs structure which we will allocate */
  71. /* ourself with help of the AllocDosObject() function:            */
  72. struct RDArgs *my_rdargs;
  73.  
  74.  
  75.  
  76. /* Declared our own functions: */
  77.  
  78. /* Our main function: */
  79. int main( int argc, char *argv[] );
  80.  
  81. /* Cleans up nicely after us: */
  82. void clean_up( STRPTR text, int code );
  83.  
  84.  
  85.  
  86. /* Main function: */
  87.  
  88. int main( int argc, char *argv[] )
  89. {
  90.   /* Simple loop variable: */
  91.   int loop;
  92.  
  93.   /* A pointer to the volume value: */
  94.   LONG *volume_value;
  95.  
  96.   /* Store the pointer which is returned by ReadArgs() here: */
  97.   struct RDArgs *temp_rdargs;
  98.  
  99.   /* The ReadArgs() function needs an arrya of LONGs where */
  100.   /* the result of the command parsing will be placed. One */
  101.   /* LONG variable is needed for every command template.   */
  102.   LONG arg_array[ NUMBER_COMMAND_TEMPLATES ];
  103.  
  104.   /* Here is our own command line we want to parse: */
  105.   /* Note the new line character ("\n") at the end  */
  106.   /* of the string. You must always include this    */
  107.   /* at the end of the strings you want to parse.   */
  108.   UBYTE *my_command_line = "Bird.snd Volume=35 Filter\n";
  109.  
  110.  
  111.  
  112.   /* We need dos library version 37 or higher: */
  113.   if( DOSBase->dl_lib.lib_Version < 37 )
  114.     clean_up( "This program needs Dos Library V37 or higher!", 20 );
  115.  
  116.  
  117.  
  118.   /* We will now clear the "arg_array" (set all values to zero): */
  119.   for( loop = 0; loop < NUMBER_COMMAND_TEMPLATES; loop++ )
  120.     arg_array[ loop ] = 0;
  121.  
  122.  
  123.  
  124.   /* Get a RDArgs structure from AmigaDOS: (We want a RDArgs */
  125.     /* structure with no special tags.)                        */
  126.   my_rdargs = (struct RDArgs *) AllocDosObject( DOS_RDARGS, NULL );
  127.  
  128.   /* Did we get a RDArgs structure: */
  129.   if( !my_rdargs )
  130.     clean_up( "Could not get a RDArgs structure!", 21 );
  131.  
  132.  
  133.  
  134.   /* Prepare the RDArgs structure so it uses our own command line: */
  135.  
  136.   /* Give the RDArgs structure our own command line: (The command */
  137.   /* line will be fetched from the CSource structure if it is not */
  138.   /* empty. Normally the command line is fetched from the default */
  139.   /* input stream which was set up when the program started, but  */
  140.   /* you may want to parse some other string rather than the one  */
  141.   /* which was written when the user launched this program).      */
  142.   my_rdargs->RDA_Source.CS_Buffer = my_command_line;
  143.  
  144.   /* Set the length of the command line: */
  145.   my_rdargs->RDA_Source.CS_Length = strlen( my_command_line );
  146.  
  147.   /* Set the current character position so it starts to read */
  148.   /* the first character in the string (character 0):        */
  149.   my_rdargs->RDA_Source.CS_CurChr = 0;
  150.  
  151.  
  152.  
  153.   /* Parse the command line: (Note that we now use our */
  154.   /* own RDArgs structure which we have prepared with  */
  155.   /* our own customized command line.)                 */
  156.   temp_rdargs = 
  157.     ReadArgs( MY_COMMAND_LINE_TEMPLATE,
  158.               arg_array,
  159.               my_rdargs
  160.             );
  161.  
  162.   /* Have AmigaDOS successfully parsed our command line? */
  163.   if( !temp_rdargs )
  164.     clean_up( "Could not parse the command line!", 22 );
  165.  
  166.  
  167.  
  168.   /* The comand line has successfully been parsed! */
  169.   /* We can now examine the "arg_array":           */
  170.  
  171.   /* Print template 1, the file name: */
  172.   if( arg_array[ SOUNDFILE_TEMPLATE ] )
  173.     printf( "File name: %s\n", arg_array[ SOUNDFILE_TEMPLATE ] );
  174.  
  175.  
  176.  
  177.   /* Print templat 2, the volume: */
  178.   if( arg_array[ VOLUME_TEMPLATE ] )
  179.   {
  180.     /* Get a pointer to the volume value: */
  181.     volume_value = (LONG *) arg_array[ VOLUME_TEMPLATE ];
  182.  
  183.     /* Print the volume: */
  184.     printf( "Volume: %ld\n", *volume_value );
  185.   }
  186.   else
  187.     printf( "No volume was set\n" );
  188.  
  189.  
  190.  
  191.   /* Print template 2, the filter switch: */
  192.   if( arg_array[ FILTER_TEMPLATE ] )
  193.     printf( "The sound filter was turned on!\n" );
  194.   else
  195.     printf( "No sound filter will be used!\n" );
  196.  
  197.  
  198.  
  199.   /* Before our program terminates we have to free the data that */
  200.   /* have been allocated when we successfully called ReadArgs(): */
  201.   FreeArgs( my_rdargs );
  202.  
  203.   /* The RDArgs structure we allocated will be */
  204.   /* deallocated in the clean_up() function.   */
  205.  
  206.   /* Clean up and exit with a smile on your face! */
  207.   clean_up( "The End", 0 );
  208. }
  209.  
  210.  
  211.  
  212. /* Handy function which closes and deallocates everything */
  213. /* that you have previously opened or allocated. You can  */
  214. /* call this function at any time, and it will clean up   */
  215. /* nicely after you and quit.                             */
  216.  
  217. void clean_up( STRPTR text, int code )
  218. {
  219.   /* Return the RDArgs structure to AmigaDOS: */
  220.   if( my_rdargs )
  221.     FreeDosObject( DOS_RDARGS, my_rdargs );
  222.  
  223.   /* Print the last message: */
  224.   printf( "%s\n", text );
  225.  
  226.   /* Quit: */
  227.   exit( code );
  228. }
  229.  
  230.  
  231.